﻿using System.Text;
using System.Web.UI.WebControls;
using FFIV.Lib.Model;

namespace FFIV.Lib.WebControl
{
	public class HeaderControl : Literal
	{
		private string m_Title;
		private string m_Description;
		private string m_Keywords;

		public string Title
		{
			get { return m_Title; }
			set
			{
				m_Title = value;
			}
		}

		public string Description
		{
			get { return m_Description; }
			set
			{
				m_Description = value;
				RefreshInnerHtml();
			}
		}

		public string Keywords
		{
			get { return m_Keywords; }
			set
			{
				m_Keywords = value;
				RefreshInnerHtml();
			}
		}

		public HtmlHeaderInfo HeaderInfo
		{
			set
			{
				Title = value.Title;
				Description = value.Description;
				Keywords = value.Keywords;
			}
		}

		public HeaderControl()
		{
			RefreshInnerHtml();

			Load += HeaderControl_Load;
		}

		private void HeaderControl_Load(object sender, System.EventArgs e)
		{
			RefreshTitle(m_Title);
		}

		private void RefreshInnerHtml()
		{
			string description = string.Format("<meta name=\"description\" content=\"{0}\">", m_Description);
			string keywords = string.Format("<meta name=\"keywords\" content=\"{0}\">", m_Keywords);


			StringBuilder sb = new StringBuilder();

			//sb.Append(title);
			sb.Append(description);
			sb.Append(keywords);

			Text = sb.ToString();
		}

		private void RefreshTitle(string title)
		{
			if (title == "首页")
			{
				Page.Title = "放飞技术网";
			}
			else
			{
				Page.Title = title + " - 放飞技术网";
			}
		}
	}
}
